#include "SPDeathMatch.h"


//----------------------------------------------------------
// GStatic
//-----------------------------------------------------------
namespace SPG
{
	GStatic* GGetStatic()
	{	
		static GStatic* g_pStatic = NULL;
		if ( !g_pStatic )
		{
			g_pStatic = new GStatic();
			g_pStatic->LoadResource();
		}
		return g_pStatic;
	}

	void GStatic::LoadResource()
	{
		//m_pPSPlayerPawn = new SPParticleSystem( L"Mesh\\playerpawn.spf" );
		//m_pPSWorld = new SPParticleSystem( L"Mesh\\land.spf" );
	}

	void GStatic::ReleaseResource()
	{

	}

}


//----------------------------------------------------------
// GWorld
//-----------------------------------------------------------
namespace SPG
{
	GWorld::GWorld()
	{
		//SpawnEmittters( GGetStatic()->m_pPSWorld );
		//SpawnEmittters( GGetStatic()->m_pPSPlayerPawn );
		LoadGame( new GDeathMatch() );
	}
}


//----------------------------------------------------------
// GDeathMatch
//-----------------------------------------------------------
namespace SPG
{
	GDeathMatch::GDeathMatch()
	{
	//	RemoveChild( m_pPlayerController );
	//	m_pPlayerController = SpawnPlayer();
	//	AddChild( m_pPlayerController );
	}

	GDeathMatch::~GDeathMatch()
	{

	}

	void GDeathMatch::StartMatch()
	{

	}

	void GDeathMatch::EndMatch()
	{

	}

	SPPlayerController* GDeathMatch::SpawnPlayer()
	{
		return new GPlayerController();
	}
}


//----------------------------------------------------------
// GPawn
//-----------------------------------------------------------
namespace SPG
{
	GPlayerPawn::GPlayerPawn(  GPlayerController* pController )
		: SPPawn( pController )
	{
		m_pGPlayerController = pController;

		//SpawnEmittters( GGetStatic()->m_pPSPlayerPawn );
	}

	void GPlayerPawn::OnAdvance( float fElapsedTime )
	{
		TransformEmitter( m_pGPlayerController->ComputeTransformMatrix() );
	}
}


//----------------------------------------------------------
// GPlayerController
//-----------------------------------------------------------
namespace SPG
{
	GPlayerController::GPlayerController()
	{

	}

	void GPlayerController::OnAdvance( float fElapsedTime )
	{
		SPPlayerController::OnAdvance( fElapsedTime );
		
		FLOAT fScaleX = 1.0f;
		FLOAT fSacleY = 1.0f;
		FLOAT fRotateRateX = 15.0f;
		FLOAT fRotateRateY = 15.0f;
		FLOAT fRotateRateZ = 0.80f;

		SPInput* pInput = m_pWorld->m_pInput;

		FLOAT x = (FLOAT)pInput->GetXClient() / SPGetCamera()->GetWindowsWidth();
		FLOAT y = (FLOAT)pInput->GetYClient() / SPGetCamera()->GetWindowsHeight();

		x = x * 2.0f - 1.0f;
		y = y * 2.0f - 1.0f;

		FLOAT xdelta = fRotateRateX * x;
		FLOAT ydelta = fRotateRateX * y;
		FLOAT zdelta = fRotateRateZ * pInput->GetXClientDelta();

		XMVECTOR vDir = XMLoadFloat3( &m_vDirection );
		XMVECTOR vZ;
		XMVECTOR vY;
		XMVECTOR vX;
		GetAxes( vDir, vX, vY, vZ );

		if ( XMVectorGetY( vDir ) > 0.9 && ydelta < 0 )
			 ydelta = 0;
		if ( XMVectorGetY( vDir ) < -0.9 && ydelta > 0 )
			ydelta = 0;
			

		XMVECTOR qPitch = XMQuaternionRotationAxis( vX, ydelta * -0.002 );
		XMVECTOR qYaw = XMQuaternionRotationRollPitchYaw( 0.0f, xdelta * 0.002, 0.0f );
		XMVECTOR qRoll = XMQuaternionRotationAxis( vDir, zdelta * -0.002 );

		XMVECTOR qRotation = XMQuaternionMultiply( qPitch, qYaw );
		qRotation = XMQuaternionMultiply( qRotation, qRoll );
		Rotate( qRotation );

		XMVECTOR vLoc = XMLoadFloat3( &m_vLocation );
		if( pInput->IsKeyDown( 'W' ) )
			vLoc += 0.5f * vZ;
		if( pInput->IsKeyDown( 'S' ) )
			vLoc -= 0.5f * vZ;
		if( pInput->IsKeyDown( 'A' ) )
			vLoc += 0.5f * vX;
		if( pInput->IsKeyDown( 'D' ) )
			vLoc -= 0.5f * vX;;

		XMStoreFloat3( &m_vLocation, vLoc );
	}

	SPPawn* GPlayerController::SpawnPawn()
	{
		m_pGPlayerPawn = new GPlayerPawn( this );
		return m_pGPlayerPawn;
	}

	SPPlayerCamera* GPlayerController::SpawnPlayerCamera()
	{
		return new GPlayerCamera( this );
	}
}

//----------------------------------------------------------
// GPlayerCamera
//-----------------------------------------------------------
namespace SPG
{
	GPlayerCamera::GPlayerCamera( GPlayerController* pOwnerController )
		: SPPlayerCamera( pOwnerController )
	{
		m_pGPlayerController = pOwnerController;
	}

	void GPlayerCamera::OnAdvance( float fElapsedTime )
	{
		
		XMVECTOR vCamOffset = XMVectorSet( 1, 0, -1, 0 );

		XMVECTOR vEye = XMLoadFloat3( &m_pGPlayerController->m_vLocation );
		XMVECTOR vDir = XMLoadFloat3( &m_pGPlayerController->m_vDirection );
		XMVECTOR vDirX, vDirY, vDirZ;
		GetAxes( vDir, vDirX, vDirY, vDirZ );
		
		vEye += vDir * -20.0f + vDirY * -6.0f;
		XMVECTOR vAt = vEye + vDir * 2000.0f;


		XMStoreFloat3( &m_vEye, vEye );
		XMStoreFloat3( &m_vLookAt, vAt );
		m_vUp = XMFLOAT3( 0.0f, 1.0f, 0.0f );

		if ( m_pSPCamera )
			m_pSPCamera->SetCamera( m_vEye, m_vLookAt, m_vUp );
			
	}
}


namespace SPG
{
	/*
	class GHUDButton : public SPHUDObject
	{
	public:
		GHUDButton();
		~GHUDButton();

	protected:
		SPSprite m_BackGround;
		SPFontSprite m_Font;
	};

	class GHUD : public SPHUD
	{
	public:
		GHUD();
		virtual ~GHUD();

		virtual void Tick( FLOAT fElapsedTime );

	protected:

	};
	*/
}